package a66weding.com.jiehuntong.activity.toolsseat;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SeatBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.view.ConfirmDialog;
import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class ToolsSeatAddSeatInfoActivity extends SeatBaseActivity implements View.OnClickListener, OnNetResultListener ,TextWatcher{
    private SwipeBackLayout mSwipeBackLayout;
    private LinearLayout mBackLay;
    private TextView mTitleTv;
    private TextView mBackTv;
    private TextView mFunctionTv;
    private EditText mSeatNameEt;
    private EditText mSeatContentEt;
    private Button mAddSeatCommitBtn;
    private boolean isEdit;
    private int tablecount;
    private int mSeatposition;
    private Bundle seatbean;
    private SeatBean.DataBean mSeatBean;
    private String title;
    private String customerName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tools_seat_add_seat_info);
        ((GlobalApplication) getApplication()).getActivityManager().addActivity("Tools_GuestsseatActivity", this);
        mSwipeBackLayout=getSwipeBackLayout();
        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        Intent intent = getIntent();
        if(intent!=null) {
            isEdit = intent.getBooleanExtra("isedit", false);
            if(isEdit) {
                Bundle seatbean =  intent.getExtras();
                if(seatbean!=null) {
                    mSeatBean = (SeatBean.DataBean) seatbean.getSerializable("seatbean");
                }
                mSeatposition= intent.getIntExtra("position",1);

            }
            tablecount = intent.getIntExtra("tablecount", 0);
            tablecount++;
        }
        initview();
    }
    private void initview() {
        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mBackTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mTitleTv = (TextView) findViewById(R.id.tv_header_title);
        mFunctionTv = (TextView) findViewById(R.id.tv_header_function);
        mSeatNameEt = (EditText) findViewById(R.id.et_tools_seat_seatname);
        mSeatContentEt = (EditText) findViewById(R.id.et_tools_seat_seatcontent);
        mAddSeatCommitBtn = (Button) findViewById(R.id.btn_tools_seat_addseatcommit);
        if(isEdit && mSeatBean!=null) {
            mTitleTv.setText("修改：第"+(++mSeatposition)+"桌("+mSeatBean.getPeopleNumber()+"人)");
            title = mSeatBean.getTableName();
            customerName = mSeatBean.getCustomerName();
            mSeatNameEt.setText(title);
            mSeatNameEt.setSelection(title.length());
            mSeatContentEt.setText(customerName);
        }else{
            mTitleTv.setText("第"+tablecount+"桌");
        }
        mBackTv.setText("席位");
        mFunctionTv.setVisibility(View.GONE);
        mBackLay.setOnClickListener(this);
        mAddSeatCommitBtn.setOnClickListener(this);
        mSeatContentEt.addTextChangedListener(this);
        mAddSeatCommitBtn.setEnabled(false);
        mSeatNameEt.addTextChangedListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
                case R.id.llay_header_back://返回
                    if(isEdit) {
                        if (mAddSeatCommitBtn.isEnabled()) {
                            showDialog(this, "您确定放弃编辑", "确定", "取消");
                            return;
                        }else{
                            finish();
                        }
                    }else{
                        if (mAddSeatCommitBtn.isEnabled()) {
                            showDialog(this, "您确定放弃编辑", "确定", "取消");
                            return;
                        }else{
                            finish();
                        }
                    }

                break;
                case R.id.btn_tools_seat_addseatcommit://提交

                    boolean isRight= checkInput();

                    if(isRight) {
                        String seatName = mSeatNameEt.getText().toString().trim();

                        String seatContent = mSeatContentEt.getText().toString().trim();

                        if(seatContent.startsWith(",") || seatContent.startsWith("，")) {
                            seatContent = seatContent.substring(1, seatContent.length());
                        }
                        String regex = ",|，";
                        String namees[] = seatContent.split(regex);
                        int numbers=namees.length;
                        if(isEdit) {//修改

                            String url= UrlDealUtil.getRealUrl(RequestCode.UPDATE_TABLEINFO);
                            HashMap<String,String> paramsmap=new HashMap<String,String>();

                            paramsmap.put("TableName",seatName);
                            paramsmap.put("WeddingTableID",String.valueOf(mSeatBean.getWeddingTableID()));
                            paramsmap.put("CustomerName",seatContent);
                            paramsmap.put("PeopleNumber",String.valueOf(numbers));

                            HttpRequestBean seathttp = new HttpRequestBean(RequestCode.POSTEQUEST,url,this,
                                    CommonResultBean.class,RequestCode.UPDATE_TABLEINFO,paramsmap);

                            HttpHandleUtil.executeCommonPost(this,seathttp);
                            showCommitPopupWindow(this,view);

                        }else{
                            String url= UrlDealUtil.getRealUrl(RequestCode.ADD_TABLEINFO);
                            HashMap<String,String> paramsmap=new HashMap<String,String>();
                            paramsmap.put("TableName",seatName);
                            paramsmap.put("CustomerName",seatContent);
                            paramsmap.put("PeopleNumber",""+numbers);
                            HttpRequestBean seathttp = new HttpRequestBean(RequestCode.POSTEQUEST,url,this,
                                    CommonResultBean.class,RequestCode.ADD_TABLEINFO,paramsmap);
                            HttpHandleUtil.executeCommonPost(this,seathttp);
                            showCommitPopupWindow(this,view);
                        }
                    }
                break;
        }
    }

    private boolean checkInput() {
        String seatName = mSeatNameEt.getText().toString().trim();
        if(TextUtils.isEmpty(seatName)) {
            CUtils.showToast("桌名不能为空");
            return false;
        }
        String seatContent = mSeatContentEt.getText().toString().trim();
        if(seatContent.startsWith(",") || seatContent.startsWith("，")) {
            seatContent = seatContent.substring(1, seatContent.length());
        }
        String regex = ",|，";
        String namees[] = seatContent.split(regex);
        if(namees.length<1) {
            CUtils.showToast("宾客信息不能为空,姓名中不能含有空格");
            return false;
        }
        for(int i=0;i<namees.length;i++){
            String namee = namees[i];
            if(namee.equals(" ")) {
                CUtils.showToast("姓名中不能含有空格");
                return false;
            }
        }
        if(TextUtils.isEmpty(seatContent)) {
            CUtils.showToast("宾客信息不能为空");
            return false;
        }
        return true;
    }
    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if(data instanceof CommonResultBean){
            if(mCommitpop!=null){
                mCommitpop.dismiss();
            }
            CommonResultBean commonresult= (CommonResultBean) data;
            switch (commonresult.getResCode()) {
                    case "0000":
                        CUtils.showToast("信息添加成功");
                        Intent intent = getIntent();
                        intent.putExtra("result","sucess");
                        this.setResult(ToolsSeatAddSeatActivity.EDITSEAT,intent);
                        finish();
                    break;
                     case "0001":
                     case "0002":
                     case "0003":
                         CUtils.showToast("信息添加失败");
                    break;
            }
        }

    }

    @Override
    public void onError(ErrorBean errorBean) {
        if(mCommitpop!=null){
            mCommitpop.dismiss();
        }
        CUtils.showToast("数据请求错误，请重试");
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
    @Override
    public void afterTextChanged(Editable s) {
        String seatContent = mSeatContentEt.getText().toString().trim();
        String titleContent = mSeatNameEt.getText().toString().trim();
        if(TextUtils.isEmpty(seatContent) && TextUtils.isEmpty(titleContent)) {
            mTitleTv.setText("第"+tablecount+"桌(0人)");
            mAddSeatCommitBtn.setEnabled(false);
            return;
        }else if(TextUtils.isEmpty(seatContent)){
            mTitleTv.setText("第"+tablecount+"桌(0人)");
            mAddSeatCommitBtn.setEnabled(false);
            return;
        }
        if(isEdit) {
            if(title.equals(titleContent) && customerName.equals(seatContent)) {
                mAddSeatCommitBtn.setEnabled(false);
            }else{
                mAddSeatCommitBtn.setEnabled(true);
            }
        }else{
            mAddSeatCommitBtn.setEnabled(true);
        }
        if(seatContent.startsWith(",") || seatContent.startsWith("，")) {
            seatContent = seatContent.substring(1, seatContent.length());
        }
        String regex = ",|，";
        String strAry[] = seatContent.split(regex);
        int numbers=strAry.length;
        mTitleTv.setText("第"+tablecount+"桌("+numbers+"人)");
    }
    public  void showDialog(Context context, String title, String confirm, String cancel) {
        final ConfirmDialog confirmDialog = new ConfirmDialog(context, title, confirm, cancel,R.layout.pop_tools_editmsg);
        confirmDialog.show();
        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                finish();
                confirmDialog.dismiss();
            }
            @Override
            public void doCancel() {
                confirmDialog.dismiss();
            }
        });
    }
}
